package com.demomaster.quickcompat.widget.base;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.ColorStateListDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.graphics.drawable.VectorDrawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.core.content.ContextCompat;

import com.demomaster.quickcompat.R;
import com.demomaster.quickcompat.drawable.QuickBitmapDrawable;
import com.demomaster.quickcompat.drawable.QuickDrawable;
import com.demomaster.quickcompat.drawable.QuickStateListDrawable;
import com.demomaster.quickcompat.util.QDBitmapUtil;
import com.demomaster.quickcompat.util.QMUIDisplayHelper;
import com.demomaster.quickcompat.util.QMUIDrawableHelper;

import java.lang.reflect.Field;
import java.util.Arrays;


public class QuickViewUtil {
    public static final String TAG = "QuickViewUtil";

    public static Activity getActivityFromView(View view) {
        Context context = view.getContext();
        while (context instanceof ContextWrapper) {
            if (context instanceof Activity) {
                return (Activity) context;
            }
            context = ((ContextWrapper) context).getBaseContext();
        }
        return null;
    }

    public static void setTintColor(ImageView imageView, int color) {
        Drawable drawable = imageView.getDrawable();
        if (drawable != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                drawable.setTint(color);
                imageView.setImageDrawable(drawable);
            }
        }
    }

    public static ColorStateList getColorStateList(int color) {
        int[] colors = new int[]{color, color, color, color, color, color};//{ pressed, focused, normal, focused, unable, normal };
        return getColorStateList(colors);
    }

    public static ColorStateList getColorStateList(int[] colors) {
        int[][] states = new int[6][];
        states[0] = new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled};
        states[1] = new int[]{android.R.attr.state_enabled, android.R.attr.state_focused};
        states[2] = new int[]{android.R.attr.state_enabled};
        states[3] = new int[]{android.R.attr.state_focused};
        states[4] = new int[]{android.R.attr.state_window_focused};
        states[5] = new int[]{};
        return new ColorStateList(states, colors);
    }

    public static float getAttrFloatValue(Context context, int attrRes) {
        TypedValue typedValue = new TypedValue();
        context.getTheme().resolveAttribute(attrRes, typedValue, true);
        return typedValue.getFloat();
    }

    public static int getAttrColor(Context context, int attrRes) {
        TypedValue typedValue = new TypedValue();
        context.getTheme().resolveAttribute(attrRes, typedValue, true);
        return typedValue.data;
    }

    public static ColorStateList getAttrColorStateList(Context context, int attrRes) {
        TypedValue typedValue = new TypedValue();
        context.getTheme().resolveAttribute(attrRes, typedValue, true);
        return ContextCompat.getColorStateList(context, typedValue.resourceId);
    }

    public static Drawable getAttrDrawable(Context context, int attrRes) {
        int[] attrs = new int[]{attrRes};
        TypedArray ta = context.obtainStyledAttributes(attrs);
        Drawable drawable = getAttrDrawable(context, ta, 0);
        ta.recycle();
        return drawable;
    }

    public static Drawable getAttrDrawable(Context context, TypedArray typedArray, int index) {
        TypedValue value = typedArray.peekValue(index);
        if (value != null) {
            if (value.type != TypedValue.TYPE_ATTRIBUTE && value.resourceId != 0) {
                return QMUIDrawableHelper.getVectorDrawable(context, value.resourceId);
            }
        }
        return null;
    }

    public static int getAttrDimen(Context context, int attrRes) {
        TypedValue typedValue = new TypedValue();
        context.getTheme().resolveAttribute(attrRes, typedValue, true);
        return TypedValue.complexToDimensionPixelSize(typedValue.data, QMUIDisplayHelper.getDisplayMetrics(context));
    }

    public static QuickViewAttribute handleCustomAttrs(Context context, AttributeSet attrs, int defStyleAttr) {
        QuickViewAttribute viewAttribute = new QuickViewAttribute(context);
        TypedArray typedArray = viewAttribute.getContext().obtainStyledAttributes(attrs, R.styleable.QuickView, defStyleAttr, 0);
        final int N = typedArray.getIndexCount();
        for (int i = 0; i < N; i++) {
            int attr = typedArray.getIndex(i);
            if (attr == R.styleable.QuickView_quick_background) {
                Drawable drawable = typedArray.getDrawable(attr);
                viewAttribute.setBackgroundDrawable(drawable);
                //attributes.mTextColor = appearance.getColorStateList(attr)
//            }else if (attr == R.styleable.QuickView_quick_background_color) {
                //Drawable drawable = typedArray.getDrawable(attr);
                //viewAttribute.setBackgroundDrawable(drawable);
//                ColorStateList backgroundColorStateList = typedArray.getColorStateList(attr);
//                if(backgroundColorStateList!=null) {
//                    int[][] states = new int[][]{new int[]{-android.R.attr.state_enabled},  // enabled
//                            new int[]{-android.R.attr.state_pressed},  // pressed
//                            new int[]{-android.R.attr.state_selected},};  // selected
//                    int colors[] = new int[]{};
//                    colors[0]=backgroundColorStateList.getColorForState(new int[]{-android.R.attr.state_enabled}, Color.TRANSPARENT);
//                    colors[0]=backgroundColorStateList.getColorForState(new int[]{-android.R.attr.state_pressed}, Color.TRANSPARENT);
//                    colors[0]=backgroundColorStateList.getColorForState(new int[]{-android.R.attr.state_selected}, Color.TRANSPARENT);
//                    ColorStateList colorStateList = new ColorStateList(states, colors);
//                    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
//                        ColorStateListDrawable colorStateListDrawable = new ColorStateListDrawable(colorStateList);
//                        viewAttribute.setBackgroundDrawable(colorStateListDrawable);
//                    }
//                }
//            }
            } else if (attr == R.styleable.QuickView_quick_radius) {//设置全部圆角和单独设置每个圆角会存在冲突
                int mRadius = typedArray.getDimensionPixelSize(attr, 0);
                viewAttribute.setRadius(mRadius);
                viewAttribute.setRadiusLeftTop(mRadius);
                viewAttribute.setRadiusLeftBottom(mRadius);
                viewAttribute.setRadiusRightTop(mRadius);
                viewAttribute.setRadiusRightBottom(mRadius);
            } else if (attr == R.styleable.QuickView_quick_radius_leftTop) {
                int mRadius = typedArray.getDimensionPixelSize(attr, 0);
                viewAttribute.setRadiusLeftTop(mRadius);
            } else if (attr == R.styleable.QuickView_quick_radius_leftBottom) {
                int mRadius = typedArray.getDimensionPixelSize(attr, 0);
                viewAttribute.setRadiusLeftBottom(mRadius);
            } else if (attr == R.styleable.QuickView_quick_radius_rightTop) {
                int mRadius = typedArray.getDimensionPixelSize(attr, 0);
                viewAttribute.setRadiusRightTop(mRadius);
            } else if (attr == R.styleable.QuickView_quick_radius_rightBottom) {
                int mRadius = typedArray.getDimensionPixelSize(attr, 0);
                viewAttribute.setRadiusRightBottom(mRadius);
            } else if (attr == R.styleable.QuickView_quick_checked) {
                boolean mChecked = typedArray.getBoolean(attr, false);
                viewAttribute.setChecked(mChecked);
            } else if (attr == R.styleable.QuickView_quick_border_color) {
                ColorStateList colorStateList = typedArray.getColorStateList(attr);
                viewAttribute.setBorderColor(colorStateList);
            } else if (attr == R.styleable.QuickView_quick_border_width) {
                int borderWidth = typedArray.getDimensionPixelSize(attr, 0);
                viewAttribute.setBorderWidth(borderWidth);
            } else if (attr == R.styleable.QuickView_quick_badge_padding) {
                int badgePadding = typedArray.getDimensionPixelSize(attr, 0);
                viewAttribute.setBadgePadding(badgePadding);
            } else if (attr == R.styleable.QuickView_quick_badge_text) {
                String badgeText = typedArray.getString(attr);
                viewAttribute.setBadgeText(badgeText);
            } else if (attr == R.styleable.QuickView_quick_badge_textColor) {
                int badgeTextColor = typedArray.getColor(attr, 0);
                viewAttribute.setBadgeTextColor(badgeTextColor);
            } else if (attr == R.styleable.QuickView_quick_badge_textSize) {
                int badgeTextSize = typedArray.getDimensionPixelSize(attr, 0);
                viewAttribute.setBadgeTextSize(badgeTextSize);
            } else if (attr == R.styleable.QuickView_quick_badge_background) {
                Drawable drawable = typedArray.getDrawable(attr);
                viewAttribute.setBadgeBackground(drawable);
            } else if (attr == R.styleable.QuickView_quick_badge_show) {
                boolean show = typedArray.getBoolean(attr, false);
                viewAttribute.setShowBadge(show);
            } else if (attr == R.styleable.QuickView_quick_badge_gravity) {
                int gravity = typedArray.getInt(attr, 0);
                viewAttribute.setBadgeGravity(gravity);
            } else if (attr == R.styleable.QuickView_quick_square) {
                boolean isSquare = typedArray.getBoolean(attr, false);
                viewAttribute.setSquare(isSquare);
            } else if (attr == R.styleable.QuickView_quick_aspect_ratio) {//宽高比
                float aspectRatio = typedArray.getFloat(i, 0);
                viewAttribute.setAspectRatio(aspectRatio);
            } else if (attr == R.styleable.QuickView_quick_loadlayout_loadViewId) {//加载布局 中的loddingview
                int viewId = typedArray.getResourceId(attr, 0);
                viewAttribute.setLoadlayout_loadViewId(viewId);
            } else if (attr == R.styleable.QuickView_quick_loadlayout_emptyViewId) {//加载布局 中的空数据
                int viewId = typedArray.getResourceId(attr, 0);
                viewAttribute.setLoadlayout_emptyViewId(viewId);
            } else if (attr == R.styleable.QuickView_quick_loadlayout_requestErrorViewId) {//加载布局 中的请求网络错误
                int viewId = typedArray.getResourceId(attr, 0);
                viewAttribute.setLoadlayout_requestErrorViewId(viewId);
            } else if (attr == R.styleable.QuickView_quick_loadlayout_contentErrorViewId) {//加载布局 中的请求成功但数据异常
                int viewId = typedArray.getResourceId(attr, 0);
                viewAttribute.setLoadlayout_contentErrorViewId(viewId);
            } else if (attr == R.styleable.QuickView_quick_layout_widthPercent) {//百分比布局
                float value = typedArray.getFraction(R.styleable.QuickView_quick_layout_widthPercent, 1, 1,
                        -1f);
                viewAttribute.setLayout_widthPercent(value);
            } else if (attr == R.styleable.QuickView_quick_layout_heightPercent) {//百分比布局
                float value = typedArray.getFraction(R.styleable.QuickView_quick_layout_heightPercent, 1, 1,
                        -1f);
                viewAttribute.setLayout_heightPercent(value);
            } else if (attr == R.styleable.QuickView_quick_drawable_height) {//百分比布局
                int value = typedArray.getDimensionPixelOffset(R.styleable.QuickView_quick_drawable_height, -1);
                viewAttribute.setDrawableHeight(value);
            } else if (attr == R.styleable.QuickView_quick_drawable_width) {//百分比布局
                int value = typedArray.getDimensionPixelOffset(R.styleable.QuickView_quick_drawable_width, -1);
                //int value = typedArray.getDimensionPixelSize(attr,0);
                viewAttribute.setDrawableWidth(value);
            } else if (attr == R.styleable.QuickView_quick_gif) {
                int resourceId = typedArray.getResourceId(attr, 0);
                viewAttribute.setGifRes(resourceId);
            } else if (attr == R.styleable.QuickView_quick_text_warp_lines) {
                boolean warpTextLine = typedArray.getBoolean(attr, false);
                viewAttribute.setWarpTextLine(warpTextLine);
            }


        }
        typedArray.recycle();
        return viewAttribute;
    }

    public static void handleCustomAttrs(TextView textView, AttributeSet attrs) {
        Context context = textView.getContext();
        if (attrs == null) {
            return;
        }
        int drawableHeight = -1, drawableWidth = -1, drawableMargin;
        int left_drawableHeight = -1, left_drawableWidth = -1;
        int right_drawableHeight = -1, right_drawableWidth = -1;
        //设置drawble的大小
        //Drawable[] drawables = textView.getCompoundDrawables();
        Drawable[] drawables = textView.getCompoundDrawablesRelative();
        if (drawables != null) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.QDTextView);
            drawableWidth = typedArray.getDimensionPixelOffset(R.styleable.QDTextView_quick_drawable_width, drawableWidth);
            drawableHeight = typedArray.getDimensionPixelOffset(R.styleable.QDTextView_quick_drawable_height, drawableHeight);
//            left_drawableHeight = typedArray.getDimensionPixelOffset(R.styleable.QDTextView_left_drawable_width, drawableWidth);
//            left_drawableWidth = typedArray.getDimensionPixelOffset(R.styleable.QDTextView_left_drawable_height, drawableHeight);
//            right_drawableHeight = typedArray.getDimensionPixelOffset(R.styleable.QDTextView_right_drawable_width, drawableWidth);
//            right_drawableWidth = typedArray.getDimensionPixelOffset(R.styleable.QDTextView_right_drawable_height, drawableHeight);

            left_drawableHeight = drawableHeight;
            left_drawableWidth = drawableWidth;
            right_drawableHeight = drawableHeight;
            right_drawableWidth = drawableWidth;

            typedArray.recycle();
            for (Drawable drawable : drawables) {
                if (drawable != null) {
                    int w = drawableWidth;
                    int h = drawableHeight;
                    try {
                        if (w == -1 || h == -1) {//若未指定大小则使用bitmap的自身大小
                            Bitmap bitmap = QDBitmapUtil.getBitmapByDrawable(drawable);
                            if (w == -1) {
                                w = bitmap.getWidth();
                            }
                            if (h == -1) {
                                h = bitmap.getHeight();
                            }
                        }
                        drawable.setBounds(0, 0, w, h);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            //Drawable[] drawables_new = new Drawable[]{drawables[0], drawables[1], drawables[2], drawables[3]};
            for (int i = 0; i < drawables.length; i++) {
                if (drawables[i] != null) {
                    int w = left_drawableWidth;
                    int h = left_drawableHeight;
                    if (i == 0) {
                        w = left_drawableWidth;
                        h = left_drawableHeight;
                    } else if (i == 2) {
                        w = right_drawableWidth;
                        h = right_drawableHeight;
                    }
                    if (w == -1 || h == -1) {//若未指定大小则使用bitmap的自身大小
                        Bitmap bitmap = QDBitmapUtil.getBitmapByDrawable(drawables[i]);
                        if (w == -1) {
                            w = bitmap.getWidth();
                        }
                        if (h == -1) {
                            h = bitmap.getHeight();
                        }
                    }
                    drawables[i].setBounds(0, 0, w, h);
                }
                //drawables_new[i] = drawables[i];
                //textView.setCompoundDrawables(drawables1[0], drawables1[1], drawables1[2], drawables1[3]);//将改变了属性的drawable再重新设置回去
            }

            //textView.setCompoundDrawablesRelative(drawables[0], drawables[1], drawables[2], drawables[3]);//将改变了属性的drawable再重新设置回去
            //textView.setCompoundDrawables(drawables[0], drawables[1], drawables[2], drawables[3]);//将改变了属性的drawable再重新设置回去
        }


        drawables = textView.getCompoundDrawables();
        //Drawable[] drawables = textView.getCompoundDrawablesRelative();
        if (drawables != null) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.QDTextView);
            drawableWidth = typedArray.getDimensionPixelOffset(R.styleable.QDTextView_quick_drawable_width, drawableWidth);
            drawableHeight = typedArray.getDimensionPixelOffset(R.styleable.QDTextView_quick_drawable_height, drawableHeight);
//            left_drawableHeight = typedArray.getDimensionPixelOffset(R.styleable.QDTextView_left_drawable_width, drawableWidth);
//            left_drawableWidth = typedArray.getDimensionPixelOffset(R.styleable.QDTextView_left_drawable_height, drawableHeight);
//            right_drawableHeight = typedArray.getDimensionPixelOffset(R.styleable.QDTextView_right_drawable_width, drawableWidth);
//            right_drawableWidth = typedArray.getDimensionPixelOffset(R.styleable.QDTextView_right_drawable_height, drawableHeight);

            left_drawableHeight = drawableHeight;
            left_drawableWidth = drawableWidth;
            right_drawableHeight = drawableHeight;
            right_drawableWidth = drawableWidth;

            typedArray.recycle();
            for (Drawable drawable : drawables) {
                if (drawable != null) {
                    int w = drawableWidth;
                    int h = drawableHeight;
                    try {
                        if (w == -1 || h == -1) {//若未指定大小则使用bitmap的自身大小
                            Bitmap bitmap = QDBitmapUtil.getBitmapByDrawable(drawable);
                            if (w == -1) {
                                w = bitmap.getWidth();
                            }
                            if (h == -1) {
                                h = bitmap.getHeight();
                            }
                        }
                        drawable.setBounds(0, 0, w, h);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            //Drawable[] drawables_new = new Drawable[]{drawables[0], drawables[1], drawables[2], drawables[3]};
            for (int i = 0; i < drawables.length; i++) {
                if (drawables[i] != null) {
                    int w = left_drawableWidth;
                    int h = left_drawableHeight;
                    if (i == 0) {
                        w = left_drawableWidth;
                        h = left_drawableHeight;
                    } else if (i == 2) {
                        w = right_drawableWidth;
                        h = right_drawableHeight;
                    }
                    if (w == -1 || h == -1) {//若未指定大小则使用bitmap的自身大小
                        Bitmap bitmap = QDBitmapUtil.getBitmapByDrawable(drawables[i]);
                        if (w == -1) {
                            w = bitmap.getWidth();
                        }
                        if (h == -1) {
                            h = bitmap.getHeight();
                        }
                    }
                    drawables[i].setBounds(0, 0, w, h);
                }
                //drawables_new[i] = drawables[i];
                //textView.setCompoundDrawables(drawables1[0], drawables1[1], drawables1[2], drawables1[3]);//将改变了属性的drawable再重新设置回去
            }

            //textView.setCompoundDrawablesRelative(drawables[0], drawables[1], drawables[2], drawables[3]);//将改变了属性的drawable再重新设置回去
            //textView.setCompoundDrawables(drawables[0], drawables[1], drawables[2], drawables[3]);//将改变了属性的drawable再重新设置回去
        }

        /*TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.QDTextView);
        //int textSize = typedArray.getDimensionPixelSize(android.R.styleable.QDTextView_android_textSize, DisplayUtil.px2sp(textView.getContext(),textView.getTextSize()));

        int textSize = typedArray.getDimensionPixelSize(R.styleable.QDTextView_android_textSize, DisplayUtil.px2sp(textView.getContext(),textView.getTextSize()));
        typedArray.recycle();
        textView.setTextSize(textSize);*/

    }

    public static ColorStateList getStateColorsFromDrawable(Drawable drawable) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            if (drawable instanceof ColorStateListDrawable) {
                ColorStateListDrawable colorStateListDrawable = (ColorStateListDrawable) drawable;
                return colorStateListDrawable.getColorStateList();
            }
        }
        Log.i(TAG, "getStateColorsFromDrawable:" + drawable);
        if (drawable instanceof ColorDrawable) {
            ColorDrawable colorDrawable = (ColorDrawable) drawable;
            int[] states = colorDrawable.getState();
            int[][] stateSet = new int[states.length][];
            int[] colors = new int[states.length];
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && states.length > 0) {
                for (int state : states) {
                    stateSet[state] = new int[]{};
                    colors[state] = colorDrawable.getColor();
                }
                return new ColorStateList(stateSet, colors);
            }
            stateSet = new int[8][];
            stateSet[0] = new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled};
            stateSet[1] = new int[]{android.R.attr.state_enabled, android.R.attr.state_focused};
            stateSet[2] = new int[]{android.R.attr.state_enabled};
            stateSet[3] = new int[]{android.R.attr.state_focused};
            stateSet[4] = new int[]{android.R.attr.state_selected};
            stateSet[5] = new int[]{android.R.attr.state_checked};
            stateSet[6] = new int[]{android.R.attr.state_window_focused};
            stateSet[7] = new int[]{};
            colors = new int[stateSet.length];
            for (int index = 0; index < colors.length; index++) {
                colors[index] = colorDrawable.getColor();
            }
            return new ColorStateList(stateSet, colors);
        } else if (drawable instanceof StateListDrawable) {
            StateListDrawable stateListDrawable = (StateListDrawable) drawable;
            // 获取 StateListDrawable 的状态数组
            int[] states = stateListDrawable.getState();
            int[][] stateSet = new int[states.length][];
            int[] colors = new int[states.length];
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                for (int index = 0; index < states.length; index++) {
                    int state = states[index];
                    stateSet[index] = stateListDrawable.getStateSet(index);
                    Drawable drawable1 = stateListDrawable.getStateDrawable(index);
                    if (drawable1 instanceof ColorDrawable) {
                        colors[index] = ((ColorDrawable) drawable1).getColor();
                    }
                }
                return new ColorStateList(stateSet, colors);
            }
        }
        return null;
    }

    public static void draw(Canvas canvas, QuickViewImpl quickViewImpl) {
        //按照逆时针方向添加一个圆
        if (quickViewImpl.attribute.radius != 0) {//圆形//圆角
            Path path = new Path();
            Rect rect = canvas.getClipBounds();
            RectF rectF = new RectF(rect);
            path.addRoundRect(rectF, quickViewImpl.attribute.radius, quickViewImpl.attribute.radius, Path.Direction.CCW);
            //设置为在圆形区域内绘制
            canvas.clipPath(path);
        } else {
            if (quickViewImpl.attribute.radiusLeftTop > 0 || quickViewImpl.attribute.radiusLeftBottom > 0 || quickViewImpl.attribute.radiusRightTop > 0 || quickViewImpl.attribute.radiusRightBottom > 0) {
                float[] radii = new float[]{
                        quickViewImpl.attribute.radiusLeftTop, quickViewImpl.attribute.radiusLeftTop,
                        quickViewImpl.attribute.radiusRightTop, quickViewImpl.attribute.radiusRightTop,
                        quickViewImpl.attribute.radiusRightBottom, quickViewImpl.attribute.radiusRightBottom,
                        quickViewImpl.attribute.radiusLeftBottom, quickViewImpl.attribute.radiusLeftBottom
                };
                Rect rect = canvas.getClipBounds();
                RectF rectF = new RectF(rect);
                Path path = new Path();
                path.addRoundRect(rectF, radii, Path.Direction.CW); // 使用正确的方向（CW 或 CCW）
                //设置为在圆形区域内绘制
                canvas.clipPath(path);
            }
        }
    }

    public static void drawBadge(View view, Canvas canvas, QuickViewImpl quickViewImpl) {
        if (quickViewImpl.attribute.showBadge) {
            QuickBadgeView.Builder builder = new QuickBadgeView.Builder();
            builder.setAttribute(quickViewImpl.attribute);
            QuickBadgeView.drawPoint(view, canvas, builder);
        }
    }

    public static void setClickable(View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (view.isClickable()) {
                Drawable currentForeground = view.getForeground();
                if (currentForeground == null) {
                    //获取selectableItemBackground中对应的attrId
                    TypedValue typedValue = new TypedValue();
                    view.getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, typedValue, true);
                    int[] attribute = new int[]{android.R.attr.selectableItemBackground};
                    TypedArray typedArray = view.getContext().getTheme().obtainStyledAttributes(typedValue.resourceId, attribute);
                    view.setForeground(typedArray.getDrawable(0));
                    typedArray.recycle();
                }
            } else {
                // 稍后，在另一个地方，您可以这样检查
                Drawable currentForeground = view.getForeground();
                if (currentForeground != null) {
                    try {
                        // 尝试获取当前前景 Drawable 的资源 ID
                        Field field = currentForeground.getClass().getDeclaredField("mResourceId");
                        field.setAccessible(true);
                        int resourceId = field.getInt(currentForeground);

                        // 判断是否与 selectableItemBackground 的资源 ID 相同
                        if (resourceId == android.R.attr.selectableItemBackground) {
                            // 当前前景确实是 selectableItemBackground
                        }
                    } catch (NoSuchFieldException | IllegalAccessException e) {
                        e.printStackTrace();
                        // 处理异常情况
                    }
                }
            }
        }
    }

    public static Drawable getBackgroundDrawable(QuickViewImpl quickViewImpl, Drawable background) {
        if (quickViewImpl == null) {
            return background;
        }
        if (background != null) {
            if (background instanceof QuickDrawable) {
                ((QuickDrawable) background).setCornerRadii(getCornerRadii(quickViewImpl.attribute));
            } else if (background instanceof StateListDrawable) {
                QuickStateListDrawable stateListDrawable = new QuickStateListDrawable(background);
                return stateListDrawable;
            }else if(background instanceof BitmapDrawable){
                QuickBitmapDrawable bitmapDrawable = new QuickBitmapDrawable(((BitmapDrawable) background).getBitmap());
                bitmapDrawable.setCornerRadii(getCornerRadii(quickViewImpl.attribute));
                bitmapDrawable.setStroke(quickViewImpl.attribute.borderWidth, quickViewImpl.attribute.borderColor);
                return bitmapDrawable;
            }else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                if (background instanceof VectorDrawable) {
    //                    QuickStateListDrawable stateListDrawable = new QuickStateListDrawable();
    //                    stateListDrawable.addState(new int[]{-1}, background);
    //                    stateListDrawable.setCornerRadii(getCornerRadii(quickViewImpl.attribute));
    //                    return stateListDrawable;
    //                    Drawable copy = background.getConstantState().newDrawable();
                    QuickBitmapDrawable bitmapDrawable = QuickBitmapDrawable.getBitmapDrawable((VectorDrawable) background);
                    bitmapDrawable.setCornerRadii(getCornerRadii(quickViewImpl.attribute));
                    bitmapDrawable.setStroke(quickViewImpl.attribute.borderWidth, quickViewImpl.attribute.borderColor);
                    return bitmapDrawable;
    // 使用 getConstantState().newDrawable() 方法来创建一个副本
                }
            }

        }
        return background;
    }

    public static float[] getCornerRadii(QuickViewAttribute attribute) {
        float[] radii = null;
        if (attribute.radius != 0) {
            radii = new float[8];
            Arrays.fill(radii, attribute.radius);
        } else {
            radii = new float[]{
                    attribute.radiusLeftTop, attribute.radiusLeftTop,
                    attribute.radiusRightTop, attribute.radiusRightTop,
                    attribute.radiusRightBottom, attribute.radiusRightBottom,
                    attribute.radiusLeftBottom, attribute.radiusLeftBottom
            };
        }
        return radii;
    }

    //通过资源名称(例如ic_launcher)获取对应的id
    public int getId(Context context, String name) {
        Resources res = context.getResources();
//return res.getIdentifier(name,null,null);//带上地址 例如 包:type/name (org.anjoy.act:drawable/ic)
        return res.getIdentifier(name, "drawable", context.getPackageName());//名称例如 ic
    }

    //通过对应id获取相应的资源名称
    public static String getResourceNameById(Context context, int id) {
        if (id == View.NO_ID) {
            return "NO_ID";
        }
        Resources res = context.getResources();
        String str = "un_find";
        try {
            str = res.getResourceEntryName(id);//得到的是 name
//return res.getResourceName(id);//得到的是 包/type/name
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }


    public static void setBackgroundKeepingPadding(View view, Drawable drawable) {
        //setBackground(view, drawable);
        int[] padding = new int[]{view.getPaddingLeft(), view.getPaddingTop(), view.getPaddingRight(), view.getPaddingBottom()};
        setBackground(view, drawable);
        view.setPadding(padding[0], padding[1], padding[2], padding[3]);
    }

    public static void setBackgroundKeepingPadding(View view, int backgroundResId) {
        setBackgroundKeepingPadding(view, ContextCompat.getDrawable(view.getContext(), backgroundResId));
    }

    public static void setBackgroundColorKeepPadding(View view, @ColorInt int color) {
        int[] padding = new int[]{view.getPaddingLeft(), view.getPaddingTop(), view.getPaddingRight(), view.getPaddingBottom()};
        view.setBackgroundColor(color);
        view.setPadding(padding[0], padding[1], padding[2], padding[3]);
    }

    public static void setBackground(View view, Drawable drawable) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            view.setBackground(drawable);
        } else {
            view.setBackgroundDrawable(drawable);
        }
    }
}
